using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;

namespace WineCellar.Web.Helpers
{
    public class ErrorResult : ActionResult
    {
        private readonly string _tempdata;
        public ErrorResult(IEnumerable<string> tempdata)
        {
            var sb = new StringBuilder();
            sb.AppendLine("<ul>");
            foreach (var error in tempdata)
            {
                sb.AppendLine(@"<li>");
                sb.AppendLine(@"<label class=""error"">" + error + "</label>");
                sb.AppendLine(@"</li>");
            }
            sb.AppendLine("</ul>");
            _tempdata = sb.ToString();
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // 400 is the HTTP status code for bad request
            context.HttpContext.Response.StatusCode = 400;
            context.HttpContext.Response.Write(_tempdata);
        }
    }
}